package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.request.CartItemRequest;
import net.xdclass.service.CartService;
import net.xdclass.until.JsonData;
import net.xdclass.vo.CartItemVO;
import net.xdclass.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("购物车")
@RestController
@RequestMapping("/api/cart/v1")
public class CarController {
    @Autowired
    private CartService cartService;

    @ApiOperation("添加购物车")
    @PostMapping("")
    public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
        cartService.addToCart(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clear")
    public JsonData cleanMyCart() {
        cartService.clear();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("/myCart")
    public JsonData findMyCart() {
        CartVO cartVO = cartService.getMyCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("删除购物车项")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteItem(
            @ApiParam(value = "商品id", required = true)
            @PathVariable("product_id") long productId
    ) {
        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }

    @ApiOperation("修改购物车数量")
    @PostMapping("change")
    public JsonData chageItemNum(
            @ApiParam(value = "商品id")
            @RequestBody CartItemRequest cartItemRequest
    ) {
        cartService.chageItemNum(cartItemRequest);
        return JsonData.buildSuccess();
    }

    /**
     * 用于订单服务 确认订单 获取 对应商品详情信息
     *
     * @param productIdList
     * @return
     */
    @ApiOperation("确认订单 获取 对应商品详情信息")
    @PostMapping("comfirm_order_cart_items")
    public JsonData comfirmOrderCartItems(@ApiParam("商品id列表") @RequestBody List<Long> productIdList) {
        List<CartItemVO> cartItemVOS = cartService.comfirmOrderCartItems(productIdList);
        return JsonData.buildSuccess(cartItemVOS);
    }
}
